package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class NUMTO extends NormalFunction
{
  static final String[] CHINESE_NUMBER = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
  static final String[] CHINESE_NUMBER_LEVEL_STR = { "", "十", "百", "千", "万", "亿" };
  static final long[] CHINESE_NUMBER_LEVEL = { 1L, 10L, 100L, 1000L, 10000L, 100000000L };

  public Object run(Object[] paramArrayOfObject)
  {
    Object localObject2;
    if (paramArrayOfObject.length < 1)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    if (paramArrayOfObject.length > 1)
      localObject2 = paramArrayOfObject[1];
    else
      localObject2 = Boolean.FALSE;
    String str = "";
    if ((localObject1 instanceof Number) && (((Number)localObject1).longValue() >= 0L) && (localObject2 instanceof Boolean))
      try
      {
        str = numberToChinese(((Number)localObject1).longValue(), ((Boolean)localObject2).booleanValue());
      }
      catch (Exception localException)
      {
        str = Primitive.ERROR_VALUE.toString();
      }
    else
      return Primitive.ERROR_NAME;
    return str;
  }

  private String numberToChinese(long paramLong, boolean paramBoolean)
    throws Exception
  {
    String str = "";
    if (paramBoolean)
    {
      while (paramLong > 0L)
      {
        str = CHINESE_NUMBER[((int)paramLong % 10)] + str;
        paramLong /= 10L;
      }
      return str;
    }
    return standardNumberToChinese(paramLong, str);
  }

  private String standardNumberToChinese(long paramLong, String paramString)
    throws Exception
  {
    for (int i = 5; i >= 4; --i)
    {
      if (paramLong / CHINESE_NUMBER_LEVEL[i] > 0L)
        if (i != 5)
          paramString = paramString + thousandToChinese(paramLong / CHINESE_NUMBER_LEVEL[i], paramString) + CHINESE_NUMBER_LEVEL_STR[i];
        else
          paramString = standardNumberToChinese(paramLong / CHINESE_NUMBER_LEVEL[i], paramString) + CHINESE_NUMBER_LEVEL_STR[i];
      else if ((paramString.length() > 0) && (!(paramString.endsWith(CHINESE_NUMBER[0]))) && (paramLong % CHINESE_NUMBER_LEVEL[i] > 0L))
        paramString = paramString + CHINESE_NUMBER[0];
      paramLong %= CHINESE_NUMBER_LEVEL[i];
    }
    return paramString += thousandToChinese(paramLong, paramString);
  }

  private String thousandToChinese(long paramLong, String paramString)
    throws Exception
  {
    String str = "";
    if ((paramLong == 10L) && (paramString.length() == 0))
      return CHINESE_NUMBER_LEVEL_STR[1];
    if ((paramLong > 10L) && (paramLong < 20L) && (paramString.length() == 0))
      return CHINESE_NUMBER_LEVEL_STR[1] + CHINESE_NUMBER[((int)paramLong % 10)];
    for (int i = 3; i >= 0; --i)
    {
      if (paramLong / CHINESE_NUMBER_LEVEL[i] > 0L)
        str = str + CHINESE_NUMBER[(int)(paramLong / CHINESE_NUMBER_LEVEL[i])] + CHINESE_NUMBER_LEVEL_STR[i];
      else if ((str.length() > 0) && (!(str.endsWith(CHINESE_NUMBER[0]))) && (paramLong % CHINESE_NUMBER_LEVEL[i] > 0L))
        str = str + CHINESE_NUMBER[0];
      paramLong %= CHINESE_NUMBER_LEVEL[i];
    }
    return str;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "NUMTO(number,bool)或NUMTO(number):返回number的中文表示。其中bool用于选择中文表示的方式，当没有bool时采用默认方式显示。\n示例：NUMTO(2345,true)等于二三四五。\n示例：NUMTO(2345,false)等于二千三百四十五。\n示例：NUMTO(2345)等于二千三百四十五。";
  }

  public String getEN()
  {
    return "";
  }
}